import React, { Component } from "react";
import { Modal, Toast } from "antd-mobile";
import { connect } from "react-redux";
import copy from "copy-to-clipboard";
import yhTools from "@/tools/yh";

import "./index.scss";

class inviteModal extends Component {
	constructor(props) {
		super(props);
		this.state = {
			visible: false,
			inviteInfo: {} //分享数据
		};
	}
	componentDidMount() {
		this.props.onRef(this);
	}
	//打开模态框
	open() {
		this.setState({
			visible: true
		});
	}
	//关闭模态框
	close() {
		this.setState({
			visible: false
		});
	}

	//设置邀请数据
	setInviteInfo(inviteInfo) {
		this.setState({
			inviteInfo
		});
	}

	//微信分享
	weiXinShare(channel) {
		if (this.getSdkVersion() < 102) {
			Toast.info("您当前的版本过低，暂不支持分享，请更新至最新版本");
			return;
		}
		let {
			couponValue,
			shareTextRespList,
			shareUrl,
			userName
		} = this.state.inviteInfo;
		let shareInfo = "";
		if (channel == "0") {
			//朋友圈
			shareInfo = shareTextRespList.find(item => {
				return item.type == "2";
			});
		} else {
			//好友
			shareInfo = shareTextRespList.find(item => {
				return item.type == "1";
			});
		}

		let { parentTitle, subTitle } = shareInfo.jsonObject;
		let reg = /(\w*)userName(.*)couponAllValue(.*)/g; //替换文案
		parentTitle = parentTitle.replace(
			reg,
			`$1${this.nameStar(userName)}$2${couponValue}$3`
		);

		yhTools
			.wxShareWebPage({
				webpageUrl: `${shareUrl}&t=${new Date().getTime()}`,
				title: parentTitle,
				description: subTitle,
				type: "0", //0：url分享
				channel // 0:朋友圈，1:好友
			})
			.then(res => {
				console.log(res, "-----------------------------------");
				if (channel == "0") {
					Toast.success("分享朋友圈成功", 2);
				} else {
					Toast.success("分享好友成功", 2);
				}
			});
	}
	//判断版本号大于
	getSdkVersion() {
		let versionCode = this.props.getAppPlatformsDatas.sdkVersionCode;
		let num = Number(versionCode.replace(/\./g, ""));
		return num;
	}
	//姓脱敏
	nameStar(name) {
		return name.substring(0, 1);
	}
	//复制链接
	copyLink() {
		const {
			couponValue,
			shareTextRespList,
			shareUrl,
			userName
		} = this.state.inviteInfo;
		let shareInfo = shareTextRespList.find(item => {
			return item.type == "3";
		});
		let { linkTitle } = shareInfo.jsonObject;
		let reg = /(\w*)userName(.*)couponAllValue(.*)shareUrl(.*)/g; //替换文案
		linkTitle = linkTitle.replace(
			reg,
			`$1${this.nameStar(userName)}$2${couponValue}$3${shareUrl}$4`
		);
		copy(linkTitle);
		Toast.success("链接复制成功", 2);
	}
	render() {
		return (
			<Modal
				className="invite_modal"
				popup
				visible={this.state.visible}
				onClose={() => {
					this.close();
				}}
				animationType="slide-up"
			>
				<div className="invite_modal_content">
					<div className="item_box">
						<div
							className="item"
							onClick={() => {
								this.weiXinShare("1");
							}}
						>
							<img src={require("@/images/invite/invite_weixin.png")} alt="" />
							<p>微信好友</p>
						</div>
						<div
							className="item"
							onClick={() => {
								this.weiXinShare("0");
							}}
						>
							<img
								src={require("@/images/invite/invite_pengyouquan.png")}
								alt=""
							/>
							<p>微信朋友圈</p>
						</div>
						<div
							className="item"
							onClick={() => {
								this.copyLink();
							}}
						>
							<img src={require("@/images/invite/invite_copy.png")} alt="" />
							<p>复制链接</p>
						</div>
					</div>
					<div
						className="cancel"
						onClick={() => {
							this.close();
						}}
					>
						取消
					</div>
				</div>
			</Modal>
		);
	}
}

function mapStateToProps(state) {
	return state;
}

function mapDispatchToProps() {
	return {
		// getAppPlatformsDatas: options => {
		//     // 同步数据
		//     dispatch(getAppPlatformsDatas(options));
		// }
	};
}

export default connect(mapStateToProps, mapDispatchToProps)(inviteModal);
